home *** CD-ROM | disk | FTP | other *** search
Lisp/Scheme | 1988-04-07 | 4.2 KB | 117 lines | [TEXT/ttxt] |
- ;; Larry Mulcahy 1988
- ;; Function to format a text to fit into a given number of columns
-
- (provide 'format-long)
- (require 'string)
- (require 'sequence)
- (require 'character "char")
-
- (defvar *width-so-far*)
-
- ; This function is for situations (like in PROMPT-WITH-DEFAULT)
- ; where a string might occasionally get too big but is expected normally
- ; to be OK. It performs a check before calling FORMAT-LONG-TEXT to avoid
- ; the overhead of breaking the string up into words and then putting it
- ; back together again unnecessarily.
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; format-text-if-its-too-long
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun format-text-if-its-too-long (text width)
- (let ((ok (call-reduce-long-text-if-necessary text)))
- (if (> (length ok) width)
- (format-long-text text width)
- text)))
-
- ; FORMAT-LONG-TEXT takes for its first argument either a string or a
- ; list of strings. Its second argument is an integer giving the
- ; number of columns available for output. It returns a string with
- ; embedded #\newlines containing all the text from the original
- ; strings squeezed into the appropriate number of columns. In the
- ; output, each word is followed by exactly one space except a period
- ; is followed by two spaces. No right-justification is performed.
- ; This does about what the NMODE command Fill Comment does.
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; format-long-text
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun format-long-text (text width)
- (setq *width-so-far* 0)
- (let ((list-of-words
- (to-list-of-words (call-reduce-long-text-if-necessary text)))
- (result ""))
- (dolist (word list-of-words)
- (setq result
- (concatenate 'string result
- (print-word-within-line-width word
- width))))
- result))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; last-character
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun last-character (str) (char str (1- (length str))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; print-word-within-line-width
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun print-word-within-line-width (word width)
- (let* ((out-str (if (equal (last-character word) #\.)
- (concatenate 'string word " ")
- (concatenate 'string word " ")))
- (big (length out-str)))
- (setq *width-so-far* (+ *width-so-far* big))
- (if (> *width-so-far* width)
- (progn
- (setq *width-so-far* big)
- (concatenate 'string out-str *newline-string*))
- out-str)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; call-reduce-long-text-if-necessary
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun call-reduce-long-text-if-necessary (text)
- (if (listp text) (reduce-long-text text) text))
-
- ; Change a list of strings into one really big string
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; reduce-long-text
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun reduce-long-text (strings)
- (let ((big (length strings)))
- (cond
- ((= big 0) "")
- ((= big 1) (car strings))
- ((concatenate 'string
- (car strings)
- " "
- (reduce-long-text (cdr strings)))))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; to-list-of-words
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun to-list-of-words (string)
- (let ((big (length string)))
- (if (> big 0)
- (let* ((word-start (position-if-not #'whitespacep string))
- (string-minus-leading-whitespace (subseq string word-start))
- (word-end
- (let ((pos (position-if #'whitespacep
- string-minus-leading-whitespace)))
- (if pos
- (+ word-start pos -1)
- (length string-minus-leading-whitespace))))
- (word-length (1+ (- word-end word-start))))
- (cons (subseq string-minus-leading-whitespace 0 word-length)
- (to-list-of-words (subseq string (1+ word-end))))))))
-
-
-